<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import java.io.IOException;</a>
<span class="sourceLineNo">020</span><a id="line.20">import java.io.InputStream;</a>
<span class="sourceLineNo">021</span><a id="line.21">import java.util.HashMap;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.util.Map;</a>
<span class="sourceLineNo">023</span><a id="line.23">import java.util.Properties;</a>
<span class="sourceLineNo">024</span><a id="line.24">import java.util.regex.Pattern;</a>
<span class="sourceLineNo">025</span><a id="line.25"></a>
<span class="sourceLineNo">026</span><a id="line.26">/**</a>
<span class="sourceLineNo">027</span><a id="line.27"> * {@code QueryLoader} is a registry for sets of queries so</a>
<span class="sourceLineNo">028</span><a id="line.28"> * that multiple copies of the same queries aren't loaded into memory.</a>
<span class="sourceLineNo">029</span><a id="line.29"> * This implementation loads properties files filled with query name to</a>
<span class="sourceLineNo">030</span><a id="line.30"> * SQL mappings.  This class is thread safe.</a>
<span class="sourceLineNo">031</span><a id="line.31"> */</a>
<span class="sourceLineNo">032</span><a id="line.32">public class QueryLoader {</a>
<span class="sourceLineNo">033</span><a id="line.33"></a>
<span class="sourceLineNo">034</span><a id="line.34">    /**</a>
<span class="sourceLineNo">035</span><a id="line.35">     * The Singleton instance of this class.</a>
<span class="sourceLineNo">036</span><a id="line.36">     */</a>
<span class="sourceLineNo">037</span><a id="line.37">    private static final QueryLoader instance = new QueryLoader();</a>
<span class="sourceLineNo">038</span><a id="line.38"></a>
<span class="sourceLineNo">039</span><a id="line.39">    /**</a>
<span class="sourceLineNo">040</span><a id="line.40">     * Matches .xml file extensions.</a>
<span class="sourceLineNo">041</span><a id="line.41">     */</a>
<span class="sourceLineNo">042</span><a id="line.42">    private static final Pattern dotXml = Pattern.compile(".+\\.[xX][mM][lL]");</a>
<span class="sourceLineNo">043</span><a id="line.43"></a>
<span class="sourceLineNo">044</span><a id="line.44">    /**</a>
<span class="sourceLineNo">045</span><a id="line.45">     * Return an instance of this class.</a>
<span class="sourceLineNo">046</span><a id="line.46">     * @return The Singleton instance.</a>
<span class="sourceLineNo">047</span><a id="line.47">     */</a>
<span class="sourceLineNo">048</span><a id="line.48">    public static QueryLoader instance() {</a>
<span class="sourceLineNo">049</span><a id="line.49">        return instance;</a>
<span class="sourceLineNo">050</span><a id="line.50">    }</a>
<span class="sourceLineNo">051</span><a id="line.51"></a>
<span class="sourceLineNo">052</span><a id="line.52">    /**</a>
<span class="sourceLineNo">053</span><a id="line.53">     * Maps query set names to Maps of their queries.</a>
<span class="sourceLineNo">054</span><a id="line.54">     */</a>
<span class="sourceLineNo">055</span><a id="line.55">    private final Map&lt;String, Map&lt;String, String&gt;&gt; queries = new HashMap&lt;&gt;();</a>
<span class="sourceLineNo">056</span><a id="line.56"></a>
<span class="sourceLineNo">057</span><a id="line.57">    /**</a>
<span class="sourceLineNo">058</span><a id="line.58">     * QueryLoader constructor.</a>
<span class="sourceLineNo">059</span><a id="line.59">     */</a>
<span class="sourceLineNo">060</span><a id="line.60">    protected QueryLoader() {</a>
<span class="sourceLineNo">061</span><a id="line.61">        super();</a>
<span class="sourceLineNo">062</span><a id="line.62">    }</a>
<span class="sourceLineNo">063</span><a id="line.63"></a>
<span class="sourceLineNo">064</span><a id="line.64">    /**</a>
<span class="sourceLineNo">065</span><a id="line.65">     * Loads a Map of query names to SQL values.  The Maps are cached so a</a>
<span class="sourceLineNo">066</span><a id="line.66">     * subsequent request to load queries from the same path will return</a>
<span class="sourceLineNo">067</span><a id="line.67">     * the cached Map.  The properties file to load can be in either</a>
<span class="sourceLineNo">068</span><a id="line.68">     * line-oriented or XML format.  XML formatted properties files must use a</a>
<span class="sourceLineNo">069</span><a id="line.69">     * {@code .xml} file extension.</a>
<span class="sourceLineNo">070</span><a id="line.70">     *</a>
<span class="sourceLineNo">071</span><a id="line.71">     * @param path The path that the ClassLoader will use to find the file.</a>
<span class="sourceLineNo">072</span><a id="line.72">     * This is &lt;strong&gt;not&lt;/strong&gt; a file system path.  If you had a jarred</a>
<span class="sourceLineNo">073</span><a id="line.73">     * Queries.properties file in the com.yourcorp.app.jdbc package you would</a>
<span class="sourceLineNo">074</span><a id="line.74">     * pass "/com/yourcorp/app/jdbc/Queries.properties" to this method.</a>
<span class="sourceLineNo">075</span><a id="line.75">     * @throws IOException if a file access error occurs</a>
<span class="sourceLineNo">076</span><a id="line.76">     * @throws IllegalArgumentException if the ClassLoader can't find a file at</a>
<span class="sourceLineNo">077</span><a id="line.77">     * the given path.</a>
<span class="sourceLineNo">078</span><a id="line.78">     * @throws java.util.InvalidPropertiesFormatException if the XML properties file is</a>
<span class="sourceLineNo">079</span><a id="line.79">     * invalid</a>
<span class="sourceLineNo">080</span><a id="line.80">     * @return Map of query names to SQL values</a>
<span class="sourceLineNo">081</span><a id="line.81">     * @see java.util.Properties</a>
<span class="sourceLineNo">082</span><a id="line.82">     */</a>
<span class="sourceLineNo">083</span><a id="line.83">    public synchronized Map&lt;String, String&gt; load(final String path) throws IOException {</a>
<span class="sourceLineNo">084</span><a id="line.84"></a>
<span class="sourceLineNo">085</span><a id="line.85">        Map&lt;String, String&gt; queryMap = this.queries.get(path);</a>
<span class="sourceLineNo">086</span><a id="line.86"></a>
<span class="sourceLineNo">087</span><a id="line.87">        if (queryMap == null) {</a>
<span class="sourceLineNo">088</span><a id="line.88">            queryMap = this.loadQueries(path);</a>
<span class="sourceLineNo">089</span><a id="line.89">            this.queries.put(path, queryMap);</a>
<span class="sourceLineNo">090</span><a id="line.90">        }</a>
<span class="sourceLineNo">091</span><a id="line.91"></a>
<span class="sourceLineNo">092</span><a id="line.92">        return queryMap;</a>
<span class="sourceLineNo">093</span><a id="line.93">    }</a>
<span class="sourceLineNo">094</span><a id="line.94"></a>
<span class="sourceLineNo">095</span><a id="line.95">    /**</a>
<span class="sourceLineNo">096</span><a id="line.96">     * Loads a set of named queries into a Map object.  This implementation</a>
<span class="sourceLineNo">097</span><a id="line.97">     * reads a properties file at the given path.  The properties file can be</a>
<span class="sourceLineNo">098</span><a id="line.98">     * in either line-oriented or XML format.  XML formatted properties files</a>
<span class="sourceLineNo">099</span><a id="line.99">     * must use a {@code .xml} file extension.</a>
<span class="sourceLineNo">100</span><a id="line.100"></a>
<span class="sourceLineNo">101</span><a id="line.101">     * @param path The path that the ClassLoader will use to find the file.</a>
<span class="sourceLineNo">102</span><a id="line.102">     * @throws IOException if a file access error occurs</a>
<span class="sourceLineNo">103</span><a id="line.103">     * @throws IllegalArgumentException if the ClassLoader can't find a file at</a>
<span class="sourceLineNo">104</span><a id="line.104">     * the given path.</a>
<span class="sourceLineNo">105</span><a id="line.105">     * @throws java.util.InvalidPropertiesFormatException if the XML properties file is</a>
<span class="sourceLineNo">106</span><a id="line.106">     * invalid</a>
<span class="sourceLineNo">107</span><a id="line.107">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">108</span><a id="line.108">     * @return Map of query names to SQL values</a>
<span class="sourceLineNo">109</span><a id="line.109">     * @see java.util.Properties</a>
<span class="sourceLineNo">110</span><a id="line.110">     */</a>
<span class="sourceLineNo">111</span><a id="line.111">    protected Map&lt;String, String&gt; loadQueries(final String path) throws IOException {</a>
<span class="sourceLineNo">112</span><a id="line.112">        // Findbugs flags getClass().getResource as a bad practice; maybe we should change the API?</a>
<span class="sourceLineNo">113</span><a id="line.113">        final Properties props;</a>
<span class="sourceLineNo">114</span><a id="line.114">        try (InputStream in = getClass().getResourceAsStream(path)) {</a>
<span class="sourceLineNo">115</span><a id="line.115"></a>
<span class="sourceLineNo">116</span><a id="line.116">            if (in == null) {</a>
<span class="sourceLineNo">117</span><a id="line.117">                throw new IllegalArgumentException(path + " not found.");</a>
<span class="sourceLineNo">118</span><a id="line.118">            }</a>
<span class="sourceLineNo">119</span><a id="line.119">            props = new Properties();</a>
<span class="sourceLineNo">120</span><a id="line.120">            if (dotXml.matcher(path).matches()) {</a>
<span class="sourceLineNo">121</span><a id="line.121">                props.loadFromXML(in);</a>
<span class="sourceLineNo">122</span><a id="line.122">            } else {</a>
<span class="sourceLineNo">123</span><a id="line.123">                props.load(in);</a>
<span class="sourceLineNo">124</span><a id="line.124">            }</a>
<span class="sourceLineNo">125</span><a id="line.125">        }</a>
<span class="sourceLineNo">126</span><a id="line.126"></a>
<span class="sourceLineNo">127</span><a id="line.127">        // Copy to HashMap for better performance</a>
<span class="sourceLineNo">128</span><a id="line.128"></a>
<span class="sourceLineNo">129</span><a id="line.129">        @SuppressWarnings({"rawtypes", "unchecked" }) // load() always creates &lt;String,String&gt; entries</a>
<span class="sourceLineNo">130</span><a id="line.130">        final</a>
<span class="sourceLineNo">131</span><a id="line.131">        HashMap&lt;String, String&gt; hashMap = new HashMap(props);</a>
<span class="sourceLineNo">132</span><a id="line.132">        return hashMap;</a>
<span class="sourceLineNo">133</span><a id="line.133">    }</a>
<span class="sourceLineNo">134</span><a id="line.134"></a>
<span class="sourceLineNo">135</span><a id="line.135">    /**</a>
<span class="sourceLineNo">136</span><a id="line.136">     * Removes the queries for the given path from the cache.</a>
<span class="sourceLineNo">137</span><a id="line.137">     * @param path The path that the queries were loaded from.</a>
<span class="sourceLineNo">138</span><a id="line.138">     */</a>
<span class="sourceLineNo">139</span><a id="line.139">    public synchronized void unload(final String path) {</a>
<span class="sourceLineNo">140</span><a id="line.140">        this.queries.remove(path);</a>
<span class="sourceLineNo">141</span><a id="line.141">    }</a>
<span class="sourceLineNo">142</span><a id="line.142"></a>
<span class="sourceLineNo">143</span><a id="line.143">}</a>




























































</pre>
</div>
</main>
</body>
</html>
